package com.lujianfei.android_app_list.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.lujianfei.android_app_list.R
import com.lujianfei.android_app_list.base.BaseRecyclerAdapter
import com.lujianfei.android_app_list.bean.AppInfoBean

class GridAdapter: BaseRecyclerAdapter<AppInfoBean>() {

    var onItemClickListener:((appInfoBean:AppInfoBean)->Unit) ?= null
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return GridViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.adapter_grid_item,parent,false))
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is GridViewHolder) {
            holder.bindData(mData[holder.adapterPosition])
        }
        holder.itemView.setOnClickListener {
            onItemClickListener?.invoke(mData[holder.adapterPosition])
        }
    }

    class GridViewHolder(itemView: View) : BaseViewHolder(itemView) {
        fun bindData(appInfoBean: AppInfoBean) {
            val icon:ImageView = itemView.findViewById(R.id.icon)
            val title: TextView = itemView.findViewById(R.id.title)
            icon.setImageDrawable(appInfoBean.appIcon)
            title.text = appInfoBean.appName
        }
    }
}